package com.wudi.estate.controller.servicemode;

import com.wudi.estate.bean.servicemode.MailBox;
import com.wudi.estate.service.servicemode.MailBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;
import java.util.Map;

@Controller
@SessionAttributes
@RequestMapping("/mailBox")
public class MailBoxController {
    @Autowired
    private MailBoxService mailBoxService;
//    查询所有信箱
    @RequestMapping("/listUi")
    public String listUi(ModelMap map){
        List<Map<String, Object>> mailBoxes = null;
        try {
            mailBoxes = mailBoxService.selectAll();
            System.out.println(mailBoxes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("mailBoxes", mailBoxes);
        return "mailBox_list";
    }

    //    显示添加信箱的页面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map) {
        Map<String, Object> maps = null;
        try {
            maps = mailBoxService.selectMailBoxAndCommunityAndPersonnel();
            System.out.println(maps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("maps", maps);
        return "mailBox_add";
    }
//   信箱添加
    @RequestMapping("/add")
    public String add(MailBox mailBox){//使对象自动注入，id和createTime不需要
        System.out.println(mailBox);
        try {
            mailBoxService.insert(mailBox);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/mailBox/addUi";
    }

    //   信箱编辑显示
    @RequestMapping("/editUi")
    public String editUi(ModelMap map, long id){
//        System.out.println(id);
        try {
            Map<String,Object> maps= mailBoxService.selectMailBoxAndPersonnel(id);
            System.out.println(maps);
            map.addAttribute("maps",maps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "mailBox_edit";
    }
//    信箱编辑
    @RequestMapping("/edit")
    public String edit(MailBox mailBox){
        System.out.println(mailBox);
        mailBoxService.update(mailBox);
        return "redirect:/mailBox/listUi";
    }

    //    信箱删除逻辑
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            mailBoxService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(id);
        return "redirect:/mailBox/listUi";
    }
}
